package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.ui.R;

/**
 * 
 * 用户组成员列表
 * @author zhao
 *
 */
public class GroupMenberListAdapter extends BaseAdapter {
	
	List<Map> mList = new ArrayList<Map>();
	Activity mActivity;
	LayoutInflater mInflater;
	OnItemOnClickListener mClickListener;
	int mType;//1添加成员,2成员删除
	
	public interface OnItemOnClickListener {
		void onClick(View view,Object object);
	}
	
	public GroupMenberListAdapter(Activity activity,OnItemOnClickListener listener,int type) {
		this.mActivity = activity;
		this.mClickListener = listener;
		this.mInflater = activity.getLayoutInflater();
		this.mType = type;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = onCreateView(convertView);
		onBindData(position, view);
		return view;
	}
	
	private View onCreateView(View view){
		if(view != null)return view;
		ViewHolder viewHolder  = null;
		switch(mType){
		case 1://删除组成员
			 viewHolder  = new ViewHolder();
			view = mInflater.inflate(R.layout.group_menber_item,null);
			viewHolder.mCheckBox = (CheckBox)view.findViewById(R.id.choose);
			viewHolder.mImageView = (ImageView)view.findViewById(R.id.head_image);
			viewHolder.mButton = (Button)view.findViewById(R.id.delete_btn);
			viewHolder.mButton.setVisibility(View.VISIBLE);
			break;
		case 2://添加成员
			viewHolder  = new ViewHolder();
			view = mInflater.inflate(R.layout.group_menber_item,null);
			viewHolder.mCheckBox = (CheckBox)view.findViewById(R.id.choose);
			viewHolder.mImageView = (ImageView)view.findViewById(R.id.head_image);
			viewHolder.mButton = (Button)view.findViewById(R.id.delete_btn);
			viewHolder.mCheckBox.setVisibility(View.VISIBLE);
			break;
		}
		view.setTag(viewHolder);
		return view;
	}
	
	public static class ViewHolder{
		CheckBox mCheckBox;
		ImageView mImageView;
		TextView mTextView;
		Button mButton;
	}
	
	private void onBindData(final int position,final View view){
		ViewHolder viewHolder = (ViewHolder)view.getTag();
		viewHolder.mCheckBox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mClickListener != null)mClickListener.onClick(view,mList.get(position));
			}
		});
	}
	
	public void onUpdateData(List<Map> list){
		this.mList = list;
		notifyDataSetChanged();
	}

}
